package com.stx.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.stx.dao.TeacherDao;
import com.stx.domain.Teacher;


@Repository


public class TeacherDaoImpl implements TeacherDao {


	
	@Resource
	private JdbcTemplate jt;
	
	public List<Teacher> selectTeacher(){
		String sql="select * from t";
		List<Teacher> list= jt.query(sql, new info());
		return list;
	}
	
	
	public Teacher teacherLogin(int no,String pwd){

		String sql="select * from t where no=? and pwd=?";
		Object[] obj={no,pwd};
		List<Teacher> list= jt.query(sql, obj, new info());
		if(list.size()==1)
			return list.get(0);
		return null;
	}
	
	public void updateTeacher(Teacher teacher){
		String sql="update t set name=?,tel=? where no=?";
		Object[] obj={teacher.getName(),teacher.getTel(),teacher.getNo()};
		jt.update(sql, obj);
	}
	
	public void teacherDelete(int no){
		jt.update("delete from t where no='"+no+"'");
	}
	
	public int addTeacher(Teacher teacher){
		return jt.update("insert into  t values(id.nextval,tno.nextval,'"+teacher.getName()+"','123456','"+teacher.getSex()+"','"+teacher.getTel()+"')");
	}
	
	class info implements RowMapper{
		@Override
		public Object mapRow(ResultSet rs, int arg1) throws SQLException {
			Teacher teacher=new Teacher();
			teacher.setId(rs.getInt("id"));
			teacher.setNo(rs.getInt("no"));
			teacher.setPwd(rs.getString("pwd"));
			teacher.setName(rs.getString("name"));
			teacher.setSex(rs.getString("sex"));
			teacher.setTel(rs.getString("tel"));
			teacher.setBuilding(rs.getString("building"));
			return teacher;
		}
	}
	
	public int modifyPasswordByT(int no,String newpassword) {
		String sql="update t set pwd=? where no=?";
		int i=0;
		Object[] obj = {newpassword, newpassword };
		i=	jt.update(sql, obj);
		return i;
		
		
	}
	
}
